package mymobile.diagram.part;

import mymobile.diagram.edit.parts.AppMobileAppNameEditPart;
import mymobile.diagram.edit.parts.AppMobileEditPart;
import mymobile.diagram.edit.parts.ContainerEditPart;
import mymobile.diagram.edit.parts.ImageEditPart;
import mymobile.diagram.edit.parts.ImageNameEditPart;
import mymobile.diagram.edit.parts.InputEditPart;
import mymobile.diagram.edit.parts.InputNameEditPart;
import mymobile.diagram.edit.parts.LabelEditPart;
import mymobile.diagram.edit.parts.LabelNameEditPart;
import mymobile.diagram.edit.parts.ListEditPart;
import mymobile.diagram.edit.parts.ListNameEditPart;
import mymobile.diagram.edit.parts.SettingEditPart;
import mymobile.diagram.edit.parts.SettingSettingNameEditPart;
import mymobile.diagram.edit.parts.ViewEditPart;
import mymobile.diagram.edit.parts.ViewViewNameEditPart;

import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.gmf.runtime.notation.Diagram;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.gmf.tooling.runtime.structure.DiagramStructure;

/**
 * This registry is used to determine which type of visual object should be
 * created for the corresponding Diagram, Node, ChildNode or Link represented
 * by a domain model object.
 * 
 * @generated
 */
public class MyMobileVisualIDRegistry {

	/**
	 * @generated
	 */
	private static final String DEBUG_KEY = "MyMobile.diagram/debug/visualID"; //$NON-NLS-1$

	/**
	 * @generated
	 */
	public static int getVisualID(View view) {
		if (view instanceof Diagram) {
			if (ContainerEditPart.MODEL_ID.equals(view.getType())) {
				return ContainerEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		return mymobile.diagram.part.MyMobileVisualIDRegistry.getVisualID(view
				.getType());
	}

	/**
	 * @generated
	 */
	public static String getModelID(View view) {
		View diagram = view.getDiagram();
		while (view != diagram) {
			EAnnotation annotation = view.getEAnnotation("Shortcut"); //$NON-NLS-1$
			if (annotation != null) {
				return (String) annotation.getDetails().get("modelID"); //$NON-NLS-1$
			}
			view = (View) view.eContainer();
		}
		return diagram != null ? diagram.getType() : null;
	}

	/**
	 * @generated
	 */
	public static int getVisualID(String type) {
		try {
			return Integer.parseInt(type);
		} catch (NumberFormatException e) {
			if (Boolean.TRUE.toString().equalsIgnoreCase(
					Platform.getDebugOption(DEBUG_KEY))) {
				MyMobileDiagramEditorPlugin.getInstance().logError(
						"Unable to parse view type as a visualID number: "
								+ type);
			}
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static String getType(int visualID) {
		return Integer.toString(visualID);
	}

	/**
	 * @generated
	 */
	public static int getDiagramVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		if (mymobile.mobileTest.MobileTestPackage.eINSTANCE.getContainer()
				.isSuperTypeOf(domainElement.eClass())
				&& isDiagram((mymobile.mobileTest.Container) domainElement)) {
			return ContainerEditPart.VISUAL_ID;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static int getNodeVisualID(View containerView, EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		String containerModelID = mymobile.diagram.part.MyMobileVisualIDRegistry
				.getModelID(containerView);
		if (!ContainerEditPart.MODEL_ID.equals(containerModelID)) {
			return -1;
		}
		int containerVisualID;
		if (ContainerEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = mymobile.diagram.part.MyMobileVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = ContainerEditPart.VISUAL_ID;
			} else {
				return -1;
			}
		}
		switch (containerVisualID) {
		case ContainerEditPart.VISUAL_ID:
			if (mymobile.mobileTest.MobileTestPackage.eINSTANCE.getView()
					.isSuperTypeOf(domainElement.eClass())) {
				return ViewEditPart.VISUAL_ID;
			}
			if (mymobile.mobileTest.MobileTestPackage.eINSTANCE.getInput()
					.isSuperTypeOf(domainElement.eClass())) {
				return InputEditPart.VISUAL_ID;
			}
			if (mymobile.mobileTest.MobileTestPackage.eINSTANCE.getList()
					.isSuperTypeOf(domainElement.eClass())) {
				return ListEditPart.VISUAL_ID;
			}
			if (mymobile.mobileTest.MobileTestPackage.eINSTANCE.getAppMobile()
					.isSuperTypeOf(domainElement.eClass())) {
				return AppMobileEditPart.VISUAL_ID;
			}
			if (mymobile.mobileTest.MobileTestPackage.eINSTANCE.getLabel()
					.isSuperTypeOf(domainElement.eClass())) {
				return LabelEditPart.VISUAL_ID;
			}
			if (mymobile.mobileTest.MobileTestPackage.eINSTANCE.getSetting()
					.isSuperTypeOf(domainElement.eClass())) {
				return SettingEditPart.VISUAL_ID;
			}
			if (mymobile.mobileTest.MobileTestPackage.eINSTANCE.getImage()
					.isSuperTypeOf(domainElement.eClass())) {
				return ImageEditPart.VISUAL_ID;
			}
			break;
		}
		return -1;
	}

	/**
	 * @generated
	 */
	public static boolean canCreateNode(View containerView, int nodeVisualID) {
		String containerModelID = mymobile.diagram.part.MyMobileVisualIDRegistry
				.getModelID(containerView);
		if (!ContainerEditPart.MODEL_ID.equals(containerModelID)) {
			return false;
		}
		int containerVisualID;
		if (ContainerEditPart.MODEL_ID.equals(containerModelID)) {
			containerVisualID = mymobile.diagram.part.MyMobileVisualIDRegistry
					.getVisualID(containerView);
		} else {
			if (containerView instanceof Diagram) {
				containerVisualID = ContainerEditPart.VISUAL_ID;
			} else {
				return false;
			}
		}
		switch (containerVisualID) {
		case ContainerEditPart.VISUAL_ID:
			if (ViewEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (InputEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ListEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (AppMobileEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (LabelEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (SettingEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			if (ImageEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ViewEditPart.VISUAL_ID:
			if (ViewViewNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case InputEditPart.VISUAL_ID:
			if (InputNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ListEditPart.VISUAL_ID:
			if (ListNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case AppMobileEditPart.VISUAL_ID:
			if (AppMobileAppNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case LabelEditPart.VISUAL_ID:
			if (LabelNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case SettingEditPart.VISUAL_ID:
			if (SettingSettingNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		case ImageEditPart.VISUAL_ID:
			if (ImageNameEditPart.VISUAL_ID == nodeVisualID) {
				return true;
			}
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static int getLinkWithClassVisualID(EObject domainElement) {
		if (domainElement == null) {
			return -1;
		}
		return -1;
	}

	/**
	 * User can change implementation of this method to handle some specific
	 * situations not covered by default logic.
	 * 
	 * @generated
	 */
	private static boolean isDiagram(mymobile.mobileTest.Container element) {
		return true;
	}

	/**
	 * @generated
	 */
	public static boolean checkNodeVisualID(View containerView,
			EObject domainElement, int candidate) {
		if (candidate == -1) {
			//unrecognized id is always bad
			return false;
		}
		int basic = getNodeVisualID(containerView, domainElement);
		return basic == candidate;
	}

	/**
	 * @generated
	 */
	public static boolean isCompartmentVisualID(int visualID) {
		return false;
	}

	/**
	 * @generated
	 */
	public static boolean isSemanticLeafVisualID(int visualID) {
		switch (visualID) {
		case ContainerEditPart.VISUAL_ID:
			return false;
		case ViewEditPart.VISUAL_ID:
		case InputEditPart.VISUAL_ID:
		case ListEditPart.VISUAL_ID:
		case AppMobileEditPart.VISUAL_ID:
		case LabelEditPart.VISUAL_ID:
		case SettingEditPart.VISUAL_ID:
		case ImageEditPart.VISUAL_ID:
			return true;
		default:
			break;
		}
		return false;
	}

	/**
	 * @generated
	 */
	public static final DiagramStructure TYPED_INSTANCE = new DiagramStructure() {
		/**
		 * @generated
		 */
		@Override
		public int getVisualID(View view) {
			return mymobile.diagram.part.MyMobileVisualIDRegistry
					.getVisualID(view);
		}

		/**
		 * @generated
		 */
		@Override
		public String getModelID(View view) {
			return mymobile.diagram.part.MyMobileVisualIDRegistry
					.getModelID(view);
		}

		/**
		 * @generated
		 */
		@Override
		public int getNodeVisualID(View containerView, EObject domainElement) {
			return mymobile.diagram.part.MyMobileVisualIDRegistry
					.getNodeVisualID(containerView, domainElement);
		}

		/**
		 * @generated
		 */
		@Override
		public boolean checkNodeVisualID(View containerView,
				EObject domainElement, int candidate) {
			return mymobile.diagram.part.MyMobileVisualIDRegistry
					.checkNodeVisualID(containerView, domainElement, candidate);
		}

		/**
		 * @generated
		 */
		@Override
		public boolean isCompartmentVisualID(int visualID) {
			return mymobile.diagram.part.MyMobileVisualIDRegistry
					.isCompartmentVisualID(visualID);
		}

		/**
		 * @generated
		 */
		@Override
		public boolean isSemanticLeafVisualID(int visualID) {
			return mymobile.diagram.part.MyMobileVisualIDRegistry
					.isSemanticLeafVisualID(visualID);
		}
	};

}
